package com.pcjavanet.generate;

import java.lang.reflect.Field;

public class ActionClassCreateTemplate extends CreateClassTemplate {
	
	
//	public Field[] getFs() {
//		return ClassUtil.getAllNoRefFields(super.getFs());
//	}
	
	private static final String BLANK_TWO_SPACE= "  ";
	@Override
	public String createFieldsDefineStr() {
		StringBuffer buf = new StringBuffer();
		buf.append( createServiceField());
		if ( getFs().length>0){
			for(int i=0; i<getFs().length; i++	){
				String typeName = getFs()[i].getType().toString().replace("class"," ");
				buf.append(BLANK_TWO_SPACE).append("private ").append( typeName )
				.append(" ").append( getFs()[i].getName() ).append(" ;\r\n"	);
			}
		}
		buf.append(BLANK_TWO_SPACE).append("private ").append( "int start  ;\r\n"	);
		buf.append(BLANK_TWO_SPACE).append("private ").append( "int limit  ;\r\n"	);
		buf.append(BLANK_TWO_SPACE).append("private ").append( "int totalProperty  ;\r\n"	);
		buf.append(BLANK_TWO_SPACE).append("private ").append( "boolean success = true ;\r\n"	);
		buf.append(BLANK_TWO_SPACE).append("private ").append( "String  info ;\r\n"	);
		String simpleName = getCls().getSimpleName();
		String name = ClassUtil.getLowerStartName( simpleName );
		buf.append(BLANK_TWO_SPACE).append("private List<").append( simpleName ).append( ">  ").append( name).append("PageBeans  ;\r\n"	);
		return buf.toString();
	}

	private String createServiceField(){
		StringBuffer buf = new StringBuffer();
		buf.append(BLANK_TWO_SPACE).append("@Resource\r\n");
		buf.append(BLANK_TWO_SPACE).append("private ").append( this.getCls().getSimpleName()).append("Service").append(" " ).append(ClassUtil.getLowerStartName(this.getCls().getSimpleName())).append("Service;\r\n");
		return buf.toString();
	}
	
	@Override
	public String createGetSetMethodStr() {
		StringBuffer buf = new StringBuffer();
		if ( getFs().length>0)
		for(int i=0; i<getFs().length; i++	){
			Field f =getFs()[i] ;
			String typeName = f.getType().toString().replace("class"," ");
			if ( typeName.toString().equalsIgnoreCase("boolean")) {
				buf.append(BLANK_TWO_SPACE).append("public ").append(typeName).append(" is").append(f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1, f.getName().length()	)).append("() {\r\n");
				buf.append(BLANK_TWO_SPACE).append(" 	return this.").append(f.getName()).append(";\r\n");
				buf.append(BLANK_TWO_SPACE).append("}\r\n");
				
				buf.append(BLANK_TWO_SPACE).append("public  void set").append(f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1, f.getName().length()	)).append("(").append(typeName).append("  ").append(f.getName()).append(") {\r\n");
				buf.append(BLANK_TWO_SPACE).append(" 	 this.").append(f.getName()).append("=").append(f.getName()).append(";\r\n");
				buf.append(BLANK_TWO_SPACE).append("}\r\n");
			} else {
				buf.append(BLANK_TWO_SPACE).append("public  ").append(typeName).append(" get").append(f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1, f.getName().length()	)).append("() {\r\n");
				buf.append(BLANK_TWO_SPACE).append(" 	return this.").append(f.getName()).append(";\r\n");
				buf.append(BLANK_TWO_SPACE).append("}\r\n");
				
				buf.append(BLANK_TWO_SPACE).append("public void set").append(f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1, f.getName().length()	)).append("(").append(typeName).append("  ").append(f.getName()).append(") {\r\n");
				buf.append(BLANK_TWO_SPACE).append(" 	 this.").append(f.getName()).append("=").append(f.getName()).append(";\r\n");
				buf.append(BLANK_TWO_SPACE).append("}\r\n");
			}
		}
		buf.append(BLANK_TWO_SPACE).append("public  void setStart(int start) {\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	 this.start = start;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		buf.append(BLANK_TWO_SPACE).append("public  void setLimit(int limit) {\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	 this.limit = limit;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		buf.append(BLANK_TWO_SPACE).append("public  void setTotalProperty(int totalProperty) {\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	 this.totalProperty = totalProperty;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		
		String simpleName = getCls().getSimpleName();
		String name = ClassUtil.getLowerStartName( simpleName );
		buf.append(BLANK_TWO_SPACE).append("public  void set").append( simpleName ).append("PageBeans(" ).append("List<").append( simpleName ).append( "> ").append( name).append("PageBeans").append("){\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	 this.").append( name).append("PageBeans =").append( name).append("PageBeans  ;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		buf.append(BLANK_TWO_SPACE).append("public  int getStart( ) {\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	 return this.start;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		buf.append(BLANK_TWO_SPACE).append("public  int getLimit( ) {\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	 return this.limit;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		buf.append(BLANK_TWO_SPACE).append("public  int getTotalProperty( ) {\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	 return this.totalProperty;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		buf.append(BLANK_TWO_SPACE).append("public    ").append("List<").append( simpleName ).append( "> ") .append("get").append( simpleName ).append("PageBeans(){\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	 return this.").append( name).append("PageBeans   ;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		buf.append(BLANK_TWO_SPACE).append("public  String getInfo( ) {\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	 return this.info;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		buf.append(BLANK_TWO_SPACE).append("public  void setInfo(String info)").append("{\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	 this.info = info ;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		buf.append(BLANK_TWO_SPACE).append("public  boolean getSuccess( ) {\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	 return this.success;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		buf.append(BLANK_TWO_SPACE).append("public  void setSuccess(boolean success)").append("{\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	 this.success = success ;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}

	@Override
	public String createActionMethodByName(String methodName) {
		StringBuffer buf = new StringBuffer();
		buf.append(BLANK_TWO_SPACE).append("public String  ").append(methodName).append("() throws Exception{\r\n" );
		buf.append(BLANK_TWO_SPACE).append("	"+getCls().getSimpleName()).append(" bean =").append( " new ").append( getCls().getSimpleName()).append("();");
		if ( getFs().length>0)
			for(int i=0; i<getFs().length; i++	){
				Field f =getFs()[i] ;
				String typeName = f.getType().toString().replace("class"," ");
				if ( typeName.toString().equalsIgnoreCase("boolean")) {
					buf.append(BLANK_TWO_SPACE).append("public  void set").append(f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1, f.getName().length()	)).append("(").append(typeName).append("  ").append(f.getName()).append(") {\r\n");
					buf.append(BLANK_TWO_SPACE).append(" 	 this.").append(f.getName()).append("=").append(f.getName()).append(";\r\n");
					buf.append(BLANK_TWO_SPACE).append("}\r\n");
				} else {
					buf.append(BLANK_TWO_SPACE).append("public void set").append(f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1, f.getName().length()	)).append("(").append(typeName).append("  ").append(f.getName()).append(") {\r\n");
					buf.append(BLANK_TWO_SPACE).append(" 	 this.").append(f.getName()).append("=").append(f.getName()).append(";\r\n");
					buf.append(BLANK_TWO_SPACE).append("}\r\n");
				}
			}
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		
		return buf.toString();
	}

	@Override
	public String createImportStr() {
		StringBuffer buf = new StringBuffer();
		buf.append( "import java.util.List;\r\n\r\n");
		buf.append("import javax.annotation.Resource;\r\n\r\n");
		buf.append("import org.apache.struts2.convention.annotation.Action;\r\n");
		buf.append("import org.apache.struts2.convention.annotation.Namespace;\r\n");
		buf.append("import org.apache.struts2.convention.annotation.ParentPackage;\r\n");
		buf.append("import org.apache.struts2.convention.annotation.Result;\r\n");
		buf.append("import org.apache.struts2.convention.annotation.Results;\r\n");
		buf.append("import org.springframework.context.annotation.Scope;\r\n");
		buf.append("import org.springframework.stereotype.Controller;\r\n");
		buf.append("import "+this.getBasepackagepath()+".model."+this.getCls().getSimpleName()+";\r\n");
		buf.append("import "+this.getBasepackagepath()+".model.PageBean;\r\n");
		if ( getImportContent().size() > 0) {
			for(String importContent :  getImportContent() )
			buf.append(  importContent ).append("\r\n");
		}
		buf.append("import "+this.getBasepackagepath()+".service."+this.getCls().getSimpleName()+"Service;\r\n\r\n");
		buf.append("import com.opensymphony.xwork2.ActionSupport;\r\n");
		
		
		String simpleName = getCls().getSimpleName();
		String name = ClassUtil.getLowerStartName( simpleName );
		buf.append("@Namespace(\"/"+name+"\")\r\n");
		buf.append("@Controller\r\n");
		buf.append("@Scope(\"prototype\")\r\n");
		buf.append("@ParentPackage(\"json-default\")\r\n");  
		buf.append("@Results({\r\n");
		buf.append("	@Result(name = \"success\", location = \"/view/"+ this.getCls().getSimpleName() +".jsp\"),\r\n");
		buf.append("	@Result(name = \"input\", location = \"/view/"+ this.getCls().getSimpleName() +".jsp\")\r\n");
		buf.append("})\r\n");
 
		return buf.toString();
	}

	@Override
	public String createBodyBegin() {
		StringBuffer buf = new StringBuffer();
		buf.append("public class ").append( getCls().getSimpleName()).append("Action ").append(" extends ActionSupport {\r\n");
		return buf.toString();
	}

	@Override
	public String createInsertMethod() {
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = ClassUtil.getLowerStartName( simpleName );
		buf.append("  @Action(value=\"insert"+ simpleName +"\",results = {@Result(name = \"success\", type = \"json\")})\r\n");
		buf.append(BLANK_TWO_SPACE).append("public String  ").append("insert").append( simpleName ).append("() throws Exception{\r\n" );
		buf.append(BLANK_TWO_SPACE).append(" 	success = false ;\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	info = \"\" ;\r\n");
		buf.append(BLANK_TWO_SPACE).append("	"+getCls().getSimpleName()).append(" bean =").append( " new ").append(  simpleName  ).append("();\r\n");
		String[] filRefBeanName = ClassUtil.getRefBeansByRefFields( ClassUtil.getAllRefFields( getFs() ) );
		if ( getFs().length>0)
			for(int i=0; i<getFs().length; i++	){
				Field f =getFs()[i] ;
				if ( f.getName().equalsIgnoreCase("dbid")){
					continue ; 
				}
				
				boolean cf = false ; 
				for( int j = 0 ; j < filRefBeanName.length ;j++ ){
					if ( filRefBeanName[j].equalsIgnoreCase( f .getName() )){
						cf = true  ;
						break ; 
					}
				}
				if ( cf )
					continue ; 
				buf.append(BLANK_TWO_SPACE).append("	bean.set").append(ClassUtil.getUpperStartName(f.getName())).append("(this.").append(f.getName()).append("); \r\n");
			}
		buf.append("	").append(name).append("Service.insert").append("(bean);\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	success = true ;\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	info = \"Add Successful\" ;\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	return SUCCESS;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}

	@Override
	public String createQueryMethod() {
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("  @Action(value=\"query"+ simpleName +"\",results = {@Result(name = \"success\", type = \"json\")})\r\n");
		buf.append(BLANK_TWO_SPACE).append("public String  ").append("query").append( simpleName ).append("() throws Exception{\r\n" );
		buf.append(BLANK_TWO_SPACE).append("	"+ simpleName ).append(" bean =").append( " new ").append(  simpleName ).append("();\r\n");
		String[] filRefBeanName = ClassUtil.getRefBeansByRefFields( ClassUtil.getAllRefFields( getFs() ) );
		if ( getFs().length>0)
			for(int i=0; i<getFs().length; i++	){
				Field f =getFs()[i] ;
				boolean cf = false ; 
				for( int j = 0 ; j < filRefBeanName.length ;j++ ){
					if ( filRefBeanName[j].equalsIgnoreCase( f .getName() )){
						cf = true  ;
						break ; 
					}
				}
				if ( cf )
					continue ; 
				buf.append(BLANK_TWO_SPACE).append("	bean.set").append(f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1, f.getName().length()	)).append("(this.").append(f.getName()).append("); \r\n");
			}
		buf.append("	").append(name).append("Service.selectByPrimaryKey").append("(bean);\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	return SUCCESS;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}

	@Override
	public String createUpdateMethod() {
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("  @Action(value=\"update"+ simpleName +"\",results = {@Result(name = \"success\", type = \"json\")})\r\n");
		buf.append(BLANK_TWO_SPACE).append("public String  ").append("update").append( simpleName ).append("() throws Exception{\r\n" );
		buf.append(BLANK_TWO_SPACE).append(" 	success = false ;\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	info = \"\" ;\r\n");
		buf.append(BLANK_TWO_SPACE).append("	"+getCls().getSimpleName()).append(" bean =").append( " new ").append( simpleName ).append("();\r\n");
		String[] filRefBeanName = ClassUtil.getRefBeansByRefFields( ClassUtil.getAllRefFields( getFs() ) );
		if ( getFs().length>0)
			for(int i=0; i<getFs().length; i++	){
				Field f =getFs()[i] ;
				boolean cf = false ; 
				for( int j = 0 ; j < filRefBeanName.length ;j++ ){
					if ( filRefBeanName[j].equalsIgnoreCase( f .getName() )){
						cf = true  ;
						break ; 
					}
				}
				if ( cf )
					continue ;
				buf.append(BLANK_TWO_SPACE).append("	bean.set").append(f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1, f.getName().length()	)).append("(this.").append(f.getName()).append("); \r\n");
			}
//		buf.append("	").append(name).append("Service.updateByPrimaryKeySelective").append("(bean);\r\n");
		buf.append(BLANK_TWO_SPACE).append("	").append("bean =").append(name).append("Service.selectByPrimaryKey(bean)").append(";\r\n");
		if ( getFs().length>0)
			for(int i=0; i<getFs().length; i++	){
				Field f =getFs()[i] ;
				boolean cf = false ; 
				for( int j = 0 ; j < filRefBeanName.length ;j++ ){
					if ( filRefBeanName[j].equalsIgnoreCase( f .getName() )){
						cf = true  ;
						break ; 
					}
				}
				if ( cf )
					continue ; 
				buf.append(BLANK_TWO_SPACE).append("	bean.set").append(f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1, f.getName().length()	)).append("(this.").append(f.getName()).append("); \r\n");
			}
		buf.append("	").append(name).append("Service.updateByPrimaryKeySelective").append("(bean);\r\n");
		
		buf.append(BLANK_TWO_SPACE).append(" 	success = true ;\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	info = \"Update Successful\" ;\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	return SUCCESS;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}

	@Override
	public String createDeleteMethod() {
		StringBuffer buf = new StringBuffer();
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("  @Action(value=\"delete"+ simpleName +"\",results = {@Result(name = \"success\", type = \"json\")})\r\n");
		buf.append(BLANK_TWO_SPACE).append("public String  ").append("delete").append( simpleName ).append("() throws Exception{\r\n" );
		buf.append(BLANK_TWO_SPACE).append("	"+getCls().getSimpleName()).append(" bean =").append( " new ").append(  simpleName ).append("();\r\n");
		String[] filRefBeanName = ClassUtil.getRefBeansByRefFields( ClassUtil.getAllRefFields( getFs() ) );
		if ( getFs().length>0)
			for(int i=0; i<getFs().length; i++	){
				Field f =getFs()[i] ;
				boolean cf = false ; 
				for( int j = 0 ; j < filRefBeanName.length ;j++ ){
					if ( filRefBeanName[j].equalsIgnoreCase( f .getName() )){
						cf = true  ;
						break ; 
					}
				}
				if ( cf )
					continue ; 
				buf.append(BLANK_TWO_SPACE).append("	bean.set").append(f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1, f.getName().length()	)).append("(this.").append(f.getName()).append("); \r\n");
			}
		buf.append("	").append(name).append("Service.deleteByPrimaryKey").append("(bean);\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	return SUCCESS;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}

	public String createIndexMethod() {
		StringBuffer buf = new StringBuffer();
		buf.append("\r\n");
		buf.append("  @Action(\"index\")\r\n");
		buf.append(BLANK_TWO_SPACE).append("public String index() throws Exception{\r\n" );
		buf.append(BLANK_TWO_SPACE).append(" 	return SUCCESS;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		return buf.toString();
	}
	
	@Override
	public String createListMethod() {
		StringBuffer buf = new StringBuffer();
		buf.append("");
		String simpleName = getCls().getSimpleName();
		String name = simpleName.substring(0, 1).toLowerCase()+simpleName.substring(1, simpleName.length());
		buf.append("  @Action(value=\"list"+ simpleName +"\",results = {@Result(name = \"success\", type = \"json\")})\r\n");
		buf.append(BLANK_TWO_SPACE).append("public String  ").append("list").append( simpleName ).append("() throws Exception{\r\n" );
		/*
		buf.append(BLANK_TWO_SPACE).append("	"+getCls().getSimpleName()).append(" bean =").append( " new ").append(  simpleName ).append("();\r\n");
		if ( getFs().length>0)
			for(int i=0; i<getFs().length; i++	){
				Field f =getFs()[i] ;
				buf.append(BLANK_TWO_SPACE).append("	bean.set").append(f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1, f.getName().length()	)).append("(this.").append(f.getName()).append("); \r\n");
			}
		*/
		buf.append(" 	QryCondition qry = RequestUtil.getQryCondition(   (javax.servlet.http.HttpServletRequest)com.opensymphony.xwork2.ActionContext.getContext().get(org.apache.struts2.ServletActionContext.HTTP_REQUEST));");
		buf.append(" 	PageBean pageBean = new PageBean();\r\n");
		buf.append("	pageBean.setPageSize( limit );\r\n");
		buf.append("	pageBean.setStartCount( start );\r\n");
 
		buf.append("	totalProperty =").append(name).append("Service.getTotalRecords").append("( qry, pageBean );\r\n");
		buf.append("	").append( name).append("PageBeans =").append( name ).append("Service.getOnePageBeans").append("( qry, pageBean );\r\n");
		buf.append(BLANK_TWO_SPACE).append(" 	return SUCCESS;\r\n");
		buf.append(BLANK_TWO_SPACE).append("}\r\n");
		buf.append( createIndexMethod() );
		return buf.toString();
	}

	@Override
	public String createBeanServiceStr() {
		return "";
	}

}
